package com.bci.pwtz.admin.service.impl;

import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.admin.service.AdminOperateLogService;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.AdminOperateLogMapper;
import com.bci.pwtz.mysql.model.AdminOperateLog;
import com.bci.pwtz.service.CompanyService;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service
public class AdminOperateLogServiceImpl implements AdminOperateLogService
{

    
    private AdminOperateLogMapper adminOperateLogMapper;
    
    private CompanyService companyService;
    /**
     * @return the adminOperateLogMapper
     */
    public AdminOperateLogMapper getAdminOperateLogMapper()
    {
        return adminOperateLogMapper;
    }

    /**
     * @param adminOperateLogMapper the adminOperateLogMapper to set
     */
    public void setAdminOperateLogMapper(AdminOperateLogMapper adminOperateLogMapper)
    {
        this.adminOperateLogMapper = adminOperateLogMapper;
    }
    
    /**
     * @return the companyService
     */
    public CompanyService getCompanyService()
    {
        return companyService;
    }

    /**
     * @param companyService the companyService to set
     */
    public void setCompanyService(CompanyService companyService)
    {
        this.companyService = companyService;
    }

    private static final Logger _logger = Logger.getLogger(AdminOperateLogServiceImpl.class);
    
    @Override
    public int add(AdminOperateLog adminOperateLog) throws PwtzDatabaseException
    {
        int result=0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("add:adminOperateLog=" + adminOperateLog.toString());
        }
        if (adminOperateLog == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("add,adminOperateLog is null");
            }
            return 0;
        }
        try
        {
            result = adminOperateLogMapper.insertSelective(adminOperateLog);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
    
    @Override
    public PaginationList<AdminOperateLog> findLogs(int operationType,String beginTime, String endTime, Long userid, String userName,int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<AdminOperateLog> paginationList = new PaginationList<AdminOperateLog>();
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("findAdminOperateLog:|operationType=" + operationType+"|deviceType=" + "|beginTime=" + beginTime+"|endTime=" + endTime);
        }
        List<AdminOperateLog> list = null;
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            list = adminOperateLogMapper.selectLogs(operationType,beginTime,endTime,userid,userName,benginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findAdminOperateLog:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = adminOperateLogMapper.selectLogsNum(operationType,beginTime,endTime,userid,userName);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }
}
